

#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>

#include "Config.h"

using namespace std;


//Funcoes para recuperar do arquivo conf.ini
//---------------------------------------------------------------------------------------
void getParameter(string pardef, string &valdef){
	char parametro[50];
	char valor[50];
	string par;
	string val;
	bool achou = false;
	FILE *inConfig;
	inConfig = fopen("config.ini", "r");
	if(inConfig == NULL){
		cout << "Erro ao abrir o arquivo de configuracao: config.ini" << endl;
		exit(1);
	}
	while(!feof(inConfig)){
		fscanf(inConfig, "%s%s", parametro, valor);
		par.append(parametro);
		val.append(valor);
		if(par == pardef){
			valdef = val;
			achou = true;
			break;
		}
		par.clear();
		val.clear();
	}
	if(!achou){
		cout << "Erro:  Parameto: \""<< pardef << "\" nao encontrado... " << endl;
		cout << "Verifique arquivo config.ini ... " << endl;
		fclose(inConfig);	
		exit(1);
	}
	fclose(inConfig);
}

void getParameter(string pardef, uint &valdef){
	char parametro[50];
	char valor[50];
	string par;
	string val;
	bool achou = false;
	FILE *inConfig;
	inConfig = fopen("config.ini", "r");
	if(inConfig == NULL){
		cout << "Erro ao abrir o arquivo de configuracao: config.ini" << endl;
		exit(1);
	}
	while(!feof(inConfig)){
		fscanf(inConfig, "%s%s", parametro, valor);
		par.append(parametro);
		val.append(valor);
		if(par == pardef){
			stringstream(val) >> valdef;  
			achou = true;
			break;
		}
		par.clear();
		val.clear();
	}
	if(!achou){
		cout << "Erro:  Parameto: \""<< pardef << "\" nao encontrado... " << endl;
		cout << "Verifique arquivo config.ini ... " << endl;
		fclose(inConfig);	
		exit(1);
	}
	
	fclose(inConfig);
}
//---------------------------------------------------------------------------------------


//main
	const std::string Config::getFirstPagesFileName() {
		string par;
		string par_def = "getFirstPagesFileName";
		getParameter(par_def, par);
		return par;
	}
	
	const bool Config::runInitializationScript() {
		uint par;
		string par_def = "runInitializationScript";
		getParameter(par_def, par);
		bool res = (par) ? true : false;
		return res;
	}
	
	const bool Config::redirectCOutToFile() {
		uint par;
		string par_def = "redirectCOutToFile";
		getParameter(par_def, par);
		bool res = (par) ? true : false;
		return res;
	}
	
	const uint Config::getQueueThreadStackSize() {
		uint par;
		string par_def = "getQueueThreadStackSize";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getNumQueueThreads() {
		uint par;
		string par_def = "getNumQueueThreads";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getDownloaderThreadStackSize() {
		uint par;
		string par_def = "getDownloaderThreadStackSize";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getNumDownloaderThreads() { 
		uint par;
		string par_def = "getNumDownloaderThreads";
		getParameter(par_def, par);
		return par;
	}
	
	//URLNormalizer
	const std::string Config::getWellKnownFilesFileName() {
		string par;
		string par_def = "getWellKnownFilesFileName";
		getParameter(par_def, par);
		return par;
	} 

	//ManagerBerthier
	const uint Config::getTimeBetweenVisits() {
		uint par;
		string par_def = "getTimeBetweenVisits";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getUrlsPerQueue() {
		uint par;
		string par_def = "getUrlsPerQueue";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getRobotsThreadStackSize() {
		uint par;
		string par_def = "getRobotsThreadStackSize";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getMaxRobotsThreads() {
		uint par;
		string par_def = "getMaxRobotsThreads";
		getParameter(par_def, par);
		return par;
	}
	
	
	//URLDiskClass
//	const int Config::getBufferUrlPathSize() { return 230;}
//	const uint Config::getBufferUrlDomainSize() { return 60;}
//	const uint Config::getBufferUrlProtocolSize() { return 10;}
	
	//DownloadParser
	const std::string Config::getUrlRejeita() {
		string par;
		string par_def = "getUrlRejeita";
		getParameter(par_def, par);
		return par;
	}
	
	const std::string Config::getWellKnownFormatsFileName() {
		string par;
		string par_def = "getWellKnownFormatsFileName";
		getParameter(par_def, par);
		return par;
	}
	
	//Repository
	const std::string Config::getPageCountFileName() {
		string par;
		string par_def = "getPageCountFileName";
		getParameter(par_def, par);
		return par;
	}
	
	//ThreadFunctionDatabase
	const uint Config::getQueueThreadSleepTime() {
		uint par;
		string par_def = "getQueueThreadSleepTime";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getDownloaderThreadSleepTime() {
		uint par;
		string par_def = "getDownloaderThreadSleepTime";
		getParameter(par_def, par);
		return par;
	}
	
	const uint Config::getDownloaderThreadNumSleeps() {
		uint par;
		string par_def = "getDownloaderThreadNumSleeps";
		getParameter(par_def, par);
		return par;
	}
	
	
	//URLFileDealer
	const uint Config::getMaxNumUrlsFromDisk() {
		uint par;
		string par_def = "getMaxNumUrlsFromDisk";
		getParameter(par_def, par);
		return par;
	}
	
	
	//blah
	const bool Config::recordStatistics() {
		uint par;
		string par_def = "recordStatistics";
		getParameter(par_def, par);
		bool res = (par) ? true : false;
		return res;
	}






////main
//	const std::string Config::getFirstPagesFileName() { return "MC Suga"; }
//	const bool Config::runInitializationScript() { return true;}
//	const bool Config::redirectCOutToFile() { return false;}
//	const uint Config::getQueueThreadStackSize() { return 300*1024;}
//	const uint Config::getNumQueueThreads() { return 2;}
//	const uint Config::getDownloaderThreadStackSize() { return 800*1024;}
//	const uint Config::getNumDownloaderThreads() { return 200;}
//	
//	//URLNormalizer
//	const std::string Config::getWellKnownFilesFileName() { return "MC Suga";} 
//
//	//ManagerBerthier
//	const uint Config::getTimeBetweenVisits() { return 30;}
//	const uint Config::getUrlsPerQueue() { return 10;}
//	const uint Config::getRobotsThreadStackSize() { return 200*1024;}
//	const uint Config::getMaxRobotsThreads() { return 100;}
//	
//	//URLDiskClass
////	const int Config::getBufferUrlPathSize() { return 230;}
////	const uint Config::getBufferUrlDomainSize() { return 60;}
////	const uint Config::getBufferUrlProtocolSize() { return 10;}
//	
//	//DownloadParser
//	const std::string Config::getUrlRejeita() { return "()'!@{}|\\^[]`<>";}
//	const std::string Config::getWellKnownFormatsFileName() { return "MC Suga";}
//	
//	//Repository
//	const std::string Config::getPageCountFileName() { return "contadorPaginas";}
//	
//	//ThreadFunctionDatabase
//	const uint Config::getQueueThreadSleepTime() { return 2;}
//	const uint Config::getDownloaderThreadSleepTime() { return 5;}
//	const uint Config::getDownloaderThreadNumSleeps() { return 3600;}
//	
//	//URLFileDealer
//	const uint Config::getMaxNumUrlsFromDisk() { return 3000;}
//	
//	//blah
//	const bool Config::recordStatistics() { return true;}
